package com.szzg10.thinking.chapter18;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

class House implements Serializable {}

public class MyWorld implements Serializable {
	
	private String name;
	private House house;
	
	public MyWorld(String name, House house) {
		this.name = name;
		this.house = house;
	}
	
	@Override
	public String toString() {
		return name + ": " + super.toString() + "---" + house.toString();
	}
	
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//constructor
		House house = new House();
		
		MyWorld myWorld0 = new MyWorld("Peter", house);
		MyWorld myWorld1 = new MyWorld("Anne", house);
		MyWorld myWorld2 = new MyWorld("Sue", house);
		
		List<MyWorld> list0 = new ArrayList<MyWorld>();
		list0.add(myWorld0);
		list0.add(myWorld1);
		list0.add(myWorld2);
		
		List<MyWorld> list1 = new ArrayList<MyWorld>();
		list1.add(myWorld0);
		list1.add(myWorld1);
		list1.add(myWorld2);
		
		System.out.println("Consturctor---");
		System.out.println(list0);
		System.out.println(list1);
		System.out.println();
		
		//same stream
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
		objectOutputStream.writeObject(list0);
		objectOutputStream.writeObject(list1);
		objectOutputStream.flush();
		
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
		ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
		list0 = (List<MyWorld>)objectInputStream.readObject();
		list1 = (List<MyWorld>)objectInputStream.readObject();
		
		System.out.println("Same stream---");
		System.out.println(list0);
		System.out.println(list1);
		System.out.println();
		
		objectOutputStream.close();
		objectInputStream.close();
		
		//Different stream
		byteArrayOutputStream = new ByteArrayOutputStream();
		objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
		objectOutputStream.writeObject(list0);
		
		ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(byteArrayOutputStream1);
		objectOutputStream1.writeObject(list1);
		
		byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
		objectInputStream = new ObjectInputStream(byteArrayInputStream);
		list0 = (List<MyWorld>)objectInputStream.readObject();
		
		ByteArrayInputStream byteArrayInputStream1 = new ByteArrayInputStream(byteArrayOutputStream1.toByteArray());
		ObjectInputStream objectInputStream1 = new ObjectInputStream(byteArrayInputStream1);
		list1 = (List<MyWorld>)objectInputStream1.readObject();
		
		System.out.println("Different stream---");
		System.out.println(list0);
		System.out.println(list1);
		
		objectOutputStream.close();
		objectOutputStream1.close();
		objectInputStream.close();
		objectInputStream1.close();
	}
}
